package com.hlingsoft.cms.action;

import com.hlingsoft.cms.dao.*;
import com.hlingsoft.cms.entity.Goods;
import com.hlingsoft.cms.entity.Image;
import com.hlingsoft.cms.entity.SizeWithPrice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

/**
 * Created by selbylei on 2015/5/5.
 */

@Controller
@RequestMapping("/goods")
public class GoodsAction extends BaseAction {



    /**
     * 添加商品
     * @param goods
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestBody Goods goods,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            goodsDao.addGoods(goods);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 商品详情
     * @param goodsId
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail.json")
    @ResponseBody
    public Map<String, Object> detail(
            @RequestParam(value = "goodsId") long goodsId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Map<String,Object>> goodsInfoList = new ArrayList<Map<String,Object>>();
            Goods goods = goodsDao.getGoodsById(goodsId);

            //尺寸和价格
            List<SizeWithPrice> sizeWithPriceList = sizeWithPriceDao.getSizeWithPrice(goodsId);
            goods.setSizeWithPriceList(sizeWithPriceList);

            //品牌
            if (goods.getBrandId()!=0){
                goods.setBrandLogo(brandDao.getBrandById(goods.getBrandId()).getLogo());
            }
            //展示图片
            List<String> images = new ArrayList<String>();
            List<Image> imageList = imageDao.getImageByGoodsId(goodsId,1);
            for (Image image:imageList){
                images.add(image.getPath());
            }

            Map<String, Object> typeMap = new HashMap<String, Object>();
            typeMap.put("key","蛋糕类型");
            typeMap.put("description",typedao.getTypeById(goods.getTypeId()).getName());

            Map<String, Object> materialMap = new HashMap<String, Object>();
            materialMap.put("key","基本原料");
            materialMap.put("description",goods.getMaterial());

            Map<String, Object> markMap = new HashMap<String, Object>();
            markMap.put("key","备注");
            markMap.put("description",goods.getMark());

            Map<String, Object> manufacturerMap = new HashMap<String, Object>();
            manufacturerMap.put("key","生产商");
            manufacturerMap.put("description",goods.getManufacturer());

//            Map<String, Object> shippingPriceMap = new HashMap<String, Object>();
//            manufacturerMap.put("key","配送费");
//            manufacturerMap.put("description",goods.getShippingPrice());

            goodsInfoList.add(typeMap);
            goodsInfoList.add(materialMap);
            goodsInfoList.add(markMap);
            goodsInfoList.add(manufacturerMap);
//            goodsInfoList.add(shippingPriceMap);

            goods.setImages(images);
            goods.setGoodsInfoList(goodsInfoList);


            resultMap.put("goods",goods);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

}
